/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kungkhies.services.kungkher.validate;

import com.kungkhies.persistence.kungkher.KungkherDao;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author clayton
 */
@Service("kungkherValidateService")
@Transactional(propagation = Propagation.REQUIRED)
public class KungkherValidateServiceImpl implements KungkherValidateService
{
    private KungkherDao kungkherDao;
    public static final Pattern ALPHANUMERIC = Pattern.compile("[A-Za-z0-9]+");

    public KungkherValidateServiceImpl()
    {
    }

    @Override
    public boolean kungkherNameExists(String kungkherName)
    {
        return kungkherDao.kungkherNameExists(kungkherName);
    }

    @Override
    public boolean emailAddressExists(String emailAddress)
    {
        return kungkherDao.emailAddressExists(emailAddress);
    }

    @Override
    public boolean isEmailAddressValid(String emailAddress)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isKungkherNameValid(String kungkherName)
    {
        Matcher m = ALPHANUMERIC.matcher(kungkherName);
        return m.matches();
    }

    @Override
    public boolean doPasswordsMatch(String password1, String password2)
    {
        if ((password1 == null || password1.trim().equals("")) || (password2 == null || password2.trim().equals("")))
        {
            return false;
        }
        else
        {
            if (password1.equals(password2))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    @Override
    public boolean isPasswordValid(String password)
    {    
        Matcher m = ALPHANUMERIC.matcher(password);
        return m.matches();
    }

   
    public void setKungkherDao(KungkherDao kungkherDao)
    {
        this.kungkherDao = kungkherDao;
    }

    @Override
    public boolean isKungkherNameLengthValid(String kungkherName)
    {
        if (kungkherName != null
                && kungkherName.length() > 0 && kungkherName.length() < 16)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    @Override
    public boolean isPasswordLengthValid(String password)
    {
        
        if (password != null
                && password.length() > 5 && password.length() < 16)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
